package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.dto.MallOrderDetailSearch;
import com.swlz.entity.MallOrderDetail;
import com.swlz.vo.MallOrderDetailVO;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 商城订单详情
 * TableName : MALL_ORDER_DETAIL
 * ClassName : com.swlz.dao.MallOrderDetailDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-28 18:17:01
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallOrderDetailDAO {

	/**
	 * 添加一个商城订单详情
	 * 包含所有字段
	 * @param mallOrderDetail - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallOrderDetail mallOrderDetail) throws SQLException {
		if(mallOrderDetail.getOrderDetailId()==null) {
			mallOrderDetail.setOrderDetailId(DBUtil.nextId());
		}
		return DBUtil.insert(mallOrderDetail);
	}
 
	
	/**
	 * 添加一个商城订单详情
	 * 只包含非null的字段
	 * @param mallOrderDetail - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallOrderDetail mallOrderDetail) throws SQLException {
		if(mallOrderDetail.getOrderDetailId()==null) {
			mallOrderDetail.setOrderDetailId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(mallOrderDetail);
	}
	

	/**
	 * 根据主键更新一个商城订单详情
	 * 包含所有字段
	 * @param mallOrderDetail - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallOrderDetail mallOrderDetail) throws SQLException {
		return DBUtil.update(mallOrderDetail);
	}
	
	/**
	 * 根据主键更新一个商城订单详情
	 * 只包含非null的字段
	 * @param mallOrderDetail - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallOrderDetail mallOrderDetail) throws SQLException {
		return DBUtil.updateSelective(mallOrderDetail);
	}
	
	/**
	 * 根据主键删除一个商城订单详情
	 * @param orderDetailId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long orderDetailId) throws SQLException  {
		return DBUtil.execute("DELETE FROM MALL_ORDER_DETAIL WHERE ORDER_DETAIL_ID=?", orderDetailId);
	}
	
	/**
	 * 根据主键获取一个商城订单详情
	 * @param orderDetailId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallOrderDetail selectById(Long orderDetailId) throws SQLException  {
		return DBUtil.get("SELECT * FROM MALL_ORDER_DETAIL WHERE ORDER_DETAIL_ID=?", MallOrderDetail.class ,orderDetailId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param orderDetailId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallOrderDetail selectByModel(MallOrderDetail mallOrderDetail)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ORDER_DETAIL WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrderDetail,sql,params);
		return DBUtil.get(sql.toString(), MallOrderDetail.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param orderDetailId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<MallOrderDetail> selectByModel(MallOrderDetail mallOrderDetail,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ORDER_DETAIL WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrderDetail,sql,params);
		sql.append(" ORDER BY ORDER_DETAIL_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallOrderDetail.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param orderDetailId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallOrderDetail mallOrderDetail)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_ORDER_DETAIL WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrderDetail,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param orderDetailId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(MallOrderDetail mallOrderDetail,StringBuffer sql,List<Object> params) {
		if(mallOrderDetail==null){
			return;
		}

		if(mallOrderDetail.getOrderDetailId()!=null) {
			sql.append(" AND ORDER_DETAIL_ID=?");
			params.add(mallOrderDetail.getOrderDetailId());
		}

		if(mallOrderDetail.getProductId()!=null) {
			sql.append(" AND PRODUCT_ID=?");
			params.add(mallOrderDetail.getProductId());
		}

		if(mallOrderDetail.getOrderId()!=null) {
			sql.append(" AND ORDER_ID=?");
			params.add(mallOrderDetail.getOrderId());
		}

		if(mallOrderDetail.getOrderDetailNum()!=null) {
			sql.append(" AND ORDER_DETAIL_NUM=?");
			params.add(mallOrderDetail.getOrderDetailNum());
		}

		if(mallOrderDetail.getOrderDetailMoney()!=null) {
			sql.append(" AND ORDER_DETAIL_MONEY=?");
			params.add(mallOrderDetail.getOrderDetailMoney());
		}

	}
	
	
	
	/**
	 * 根据模型中的非空字段查询
	 * @param orderDetailId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<MallOrderDetailVO> select(MallOrderDetailSearch search,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT b.*,a.* FROM MALL_ORDER_DETAIL a INNER JOIN mall_product b ON a.PRODUCT_ID=b.PRODUCT_ID AND a.ORDER_ID=?");
	
		sql.append(" ORDER BY a.ORDER_DETAIL_ID DESC LIMIT ?,?");
		return DBUtil.list(sql.toString(), MallOrderDetailVO.class, search.getOrderId(),index,len);
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param orderDetailId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long count(MallOrderDetailSearch search)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_ORDER_DETAIL WHERE ORDER_ID=?");
		return DBUtil.getLong(sql.toString(), search.getOrderId());
	}
	
	
}
